/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vale.util;

import java.util.Vector;

/**
 *
 * @author juan
 */
public abstract class AttributeExtractor {

    private final static byte READING_NAME = 0;
    private final static byte READING_EQ = 1;
    private final static byte READING_VALUE = 2;

    public static Attribute[] extract(final String tag, final String start, final String end) {
        Vector<Attribute> atts = new Vector<Attribute>();
        String working = deleteStartAndEnd(tag, start, end);
        working = deleteName(working);
        byte state = READING_NAME;
        Attribute at = new Attribute();
        for (int i = 0; i < working.length(); i++) {
            char c = working.charAt(i);
            switch (state) {
                case READING_NAME:
                    if (c == '=') {
                        state = READING_EQ;
                    } else {
                        at.setName(at.getName() + String.valueOf(c));
                    }
                    break;
                case READING_EQ:
                    if (c == '"') {
                        state = READING_VALUE;
                    }
                    break;
                case READING_VALUE:
                    if (c == '"' || i == working.length() - 1) {
                        atts.add(at);
                        at = new Attribute();
                        state = READING_NAME;
                    } else {
                        at.setValue(at.getValue() + String.valueOf(c));
                    }
                    break;
                default:
                    break;
            }//switch
            if (i == working.length() - 1 && state != READING_VALUE) {
                atts.add(at);
            }//if
        }//for
        return atts.toArray(new Attribute[atts.size()]);
    }

    private static String deleteName(String working) {
        int i;
        for (i = 0; i < working.length(); i++) {
            String ct = String.valueOf(working.charAt(i));
            ct = ct.trim();
            if (ct.isEmpty()) {
                break;
            }
        }
        return working.substring(i).trim();
    }

    private static String deleteStartAndEnd(String tag, String start, String end) {
        int from = start.length();
        int to = tag.length() - end.length();
        if (to <= from) {
            return "";
        } else {
            return tag.substring(from, to).trim();
        }
    }

    public static final class Attribute {

        private String name;
        private String value;

        public Attribute(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public Attribute() {
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return this.name + "=\"" + this.value + "\"";
        }
    }
}
